﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using SieenaDashboard.Domain;
using SieenaDashboard.Domain.Model;

namespace SieenaDashboard.Domain
{
    public interface ISDService 
    {
        IEnumerable<EventItem> GetEvents();
        IEnumerable<NoticeItem> GetNotices();
        IEnumerable<PersonItem> GetPeople();
    }

    public class Service : ISDService
    {
        protected RESTSharepoint.HumanResourcesDataContext Context { get; set; }

        public Service(string domain, string user, string password, string serviceRoot)
        {
            Context = new RESTSharepoint.HumanResourcesDataContext(new Uri(serviceRoot));
            //TODO: Use Windows Credentials
            Context.Credentials = new NetworkCredential(user, password, domain);
        }

        public IEnumerable<EventItem> GetEvents() 
        {
            var photos = Context.EventPics
                .Select(p => new
                {
                    Event = p.EventId,
                    PhotoURL = p.PhotoURL.Contains(',') ? p.PhotoURL.Split(',').First() : p.PhotoURL
                }).ToList();

            var events = Context.EventData
                .Where(n => n.StartDate <= DateTime.Now && n.EndDate >= DateTime.Now)
                .Select(e => new EventItem
            {
                Id = e.Id,
                Title = e.Title,
                Date = e.Date.GetValueOrDefault(DateTime.Now),
                StartDate = e.StartDate.GetValueOrDefault(DateTime.Now),
                EndDate = e.EndDate.GetValueOrDefault(DateTime.Now)
            }).ToList();

            foreach (var item in events)
            {
                var eventPhotos = photos.Where(p => p.Event == item.Id);
                item.Photos = eventPhotos.Select(p => p.PhotoURL).ToList();
            }

            return events;
        }

        public IEnumerable<NoticeItem> GetNotices() 
        {
            return Context.Avisos
                .Where(n => n.StartDate <= DateTime.Now && n.EndDate >= DateTime.Now )
                .Select(n => new NoticeItem
            {
                Title = n.Title,
                Date = n.Date.GetValueOrDefault(DateTime.Now),
                StartDate = n.StartDate.GetValueOrDefault(DateTime.Now),
                EndDate = n.EndDate.GetValueOrDefault(DateTime.Now),
                Summary = n.Summary
            }).ToList();
        }

        public IEnumerable<PersonItem> GetPeople() 
        {
            return Context.PersonData.Where(p => !(p.PhotoURL == null) && p.PhotoURL != "").Select(p => new PersonItem
            {
                Admission = p.Admission,
                Birthday = p.Birthday,
                FullName = p.Title,
                FirstName = p.Title.Split(' ').First(),
                PhotoURL = string.IsNullOrEmpty(p.PhotoURL) ? string.Empty : p.PhotoURL.Contains(',') ? p.PhotoURL.Split(',').First() : p.PhotoURL
            }).ToList();
        }

    }
}
